package weifei.wx.community.controller;


import com.auth0.jwt.JWT;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import weifei.web.Result;
import weifei.wx.community.entity.Tie;
import weifei.wx.community.entity.dto.TieDTO;
import weifei.wx.community.entity.dto.UserDTO;
import weifei.wx.community.mapper.TieMapper;
import weifei.wx.community.service.UserService;


@RestController
@RequestMapping("/tie")
public class TieController
{
    public static final int PAGE_COUNT = 6;

    @Autowired
    private UserService userService;

    @Autowired
    private TieMapper tieMapper;



    @PostMapping("/publish")
    public Result publish(HttpServletRequest request, @RequestBody @Valid Tie tie)
    {
        String token = request.getHeader("access_token");
        Long uid = JWT.decode(token).getClaim("user_id").asLong();

        tie.setUserId(uid);
        if (this.tieMapper.insert(tie) == 0)
            return Result.failed("内部错误");
        return Result.success();
    }


    @RequestMapping("/deleteTieByID")
    public Result deleteTieByID(
            HttpServletRequest request,
            @Valid @NotNull Integer id
        )
    {
        String token = request.getHeader("access_token");
        Long uid = JWT.decode(token).getClaim("user_id").asLong();

        QueryWrapper queryWrapper = QueryWrapper.create()
                .where("user_id = ?", uid)
                .where("id = ?", id);
        if (this.tieMapper.deleteByQuery(queryWrapper) == 0)
            return Result.failed("错误");
        return Result.success();
    }


    @RequestMapping("/getTieByID")
    public Result getTieByID(@Valid @NotNull Long id)
    {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select()
                .where("id = ?", id);
        final Tie t = this.tieMapper.selectOneByQuery(queryWrapper);
        if (t == null)
            return Result.failed("不存在的帖子");

        final UserDTO userDTO = this.userService.getUserByID(t.getUserId());
        final TieDTO dto = new TieDTO(userDTO, t);

        return Result.success("获取成功", dto);
    }


    @RequestMapping("/getTieByPage")
    public Result getTieByPage(@Valid @NotNull Integer page)
    {
        QueryWrapper queryWrapper = QueryWrapper.create()
            .select()
            .orderBy("publish_time desc")
            .limit(TieController.PAGE_COUNT * (page - 1), TieController.PAGE_COUNT);
        var ties = this.tieMapper.selectListByQuery(queryWrapper);
        var tiesDTO = new TieDTO[ties.size()];

        for(int i = 0; i < ties.size(); i++)
        {
            final Long uid = ties.get(i).getUserId();
            var userDTO = this.userService.getUserByID(uid);
            tiesDTO[i] = new TieDTO(userDTO, ties.get(i));
        }
        return Result.success("获取成功", tiesDTO);
    }


    @RequestMapping("/getTieByUserIDAndPage")
    public Result getTieByUserIDAndPage(
            @Valid @NotNull Long uid,
            @Valid @NotNull Integer page
        )
    {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select()
                .where("user_id = ?", uid)
                .orderBy("publish_time desc")
                .limit(TieController.PAGE_COUNT * (page - 1), TieController.PAGE_COUNT);
        var ties = this.tieMapper.selectListByQuery(queryWrapper);
        var tiesDTO = new TieDTO[ties.size()];

        for(int i = 0; i < ties.size(); i++)
        {
            tiesDTO[i] = new TieDTO(null, ties.get(i));
        }
        return Result.success("获取成功", tiesDTO);
    }


    @RequestMapping("/searchTieByPage")
    public Result searchTieByPage(
            @Valid @NotNull @NotBlank @NotEmpty String searchValue,
            @Valid @NotNull Integer page
        )
    {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select()
                // .like("user_id", searchValue)
                .like("content", searchValue)
                .orderBy("publish_time desc")
                .limit(TieController.PAGE_COUNT * (page - 1), TieController.PAGE_COUNT);
        var ties = this.tieMapper.selectListByQuery(queryWrapper);
        var tiesDTO = new TieDTO[ties.size()];

        for(int i = 0; i < ties.size(); i++)
        {
            final Long uid = ties.get(i).getUserId();
            var userDTO = this.userService.getUserByID(uid);
            tiesDTO[i] = new TieDTO(userDTO, ties.get(i));
        }
        return Result.success("获取成功", tiesDTO);
    }
}


